<?php
class MediaController extends AppController {
    var $name = 'Media';
    
    var $components = array('Image');
    
    var $uses = array('Media');

    // allowed file types    
    var $allowed_files=array('image/jpeg','image/gif','image/png','video/flv');
    
    // direct access to the media manager
    function admin_index($media_type=null,$foreign_key_id=null,$media_id=null) {
         if(!$media_type || !$foreign_key_id){
            $this->Session->setFlash('What are you looking for?');             
            $this->redirect('/cp');
            exit;
         }
         
         
        
         if(!empty($this->data)){
             $this->admin_add_media();
         }
         
         $posts = $this->paginate('Media',array("type"=>$media_type,"foreign_key_id"=>$foreign_key_id));
        
         $this->set(compact('posts'));
         
         $this->set(compact('media_type',"foreign_key_id"));
         
         
         if($media_id){
            $this->Media->id=$media_id;
            $this->data=$this->Media->read();
         }
            
            
    }
    
    
    // add a media
    function admin_add_media() {
        
        
        if(!$this->data['Media']['file_media']['error'] && !in_array($this->data['Media']['file_media']['type'],$this->allowed_files)){
            $this->Session->setFlash('Invalid Media File Format!');     
        }
        else {
            $file_id = $this->Image->upload_image_and_thumbnail($this->data['Media']['file_media'], 650, 1000, 'uploads', 'resize');
            
            if(isset($file_id) && $file_id && !isset($this->data['Media']['id'])) :
                
                $this->data['Media']['file_name']=$this->data['Media']['file_media']['name'];
                $this->data['Media']['file_media']=$file_id;
                if($this->Media->save($this->data)){
                    $this->Session->write('Success',true);
                    $this->Session->setFlash('Image Uploaded successfully!');             
                }
                else $this->Session->setFlash('File Upload Failed!');             
            else :
                if(isset($this->data['Media']['id'])){
                    $check = array('Media.id' => $this->data['Media']['id']);
            
                    $tmp=$this->Media->find($check);
                }
            
                
                if(isset($file_id) && $file_id){
                    $this->data['Media']['file_name']=$this->data['Media']['file_media']['name'];
                    $this->data['Media']['file_media']=$file_id;
                }
                else unset($this->data['Media']['file_media']); 
                
                $this->data['Media']['modified']=date('Y-m-d H:i:s');
                
                if($this->Media->save($this->data)){
                    if(isset($file_id) && $file_id && isset($tmp)){                        
                        $old_file=dirname(dirname(__FILE__)).'/webroot/img/uploads/'.$tmp['Media']['file_media'];
                        @unlink($old_file);
                    }
                    $this->Session->write('Success',true);
                    $this->Session->setFlash('Image updated successfully!');             
                }
                else $this->Session->setFlash('File Upload Failed!');             
                
                
            endif;        
        }
        $this->redirect($this->referer());
        exit;
    }
    
    
    // delete a media
    function admin_delete_media($id) {
        if($id == null) $this->redirect($this->referer());
        
        
        
        $check = array('Media.id' => $id);
        
        $tmp=$this->Media->find($check);
        
        if($this->Media->delete($check)) :
                  
                  $old_file=dirname(dirname(__FILE__)).'/webroot/img/uploads/'.$tmp['Media']['file_media'];
                  @unlink($old_file);
                  
                  $this->Session->write('Success',true);
                  $this->Session->setFlash('Image deleted sucessfully');
        endif;
        
        $this->redirect($this->referer());
        exit;
        
    }
    
    
    
    // update media order
    
    function admin_update_order() {
        
        if(!empty($this->data)){
            Configure::write('debug',0);
            
            
            $this->data['Ajax']=explode(',',$this->data['Ajax']);  
            unset($this->data['Ajax'][count($this->data['Ajax'])-1]);
            
            $data=array();
            
            foreach($this->data['Ajax'] as $key=>$item){
                    $tmp=array();
                    
                    $tmp['id']=$item;
                    $tmp['ordering']=$key;
                    $data[]=$tmp;
                
            }
            
            
            $success=true;
            unset($this->Media->validate);
            
            
            if(count($data))
                if(!$this->Media->saveAll($data))$success=false;
            
            
            
            
            if($success){
                echo 'success';
                $this->Session->write('Success',true);
                $this->Session->setFlash('News Order Updated!');
            }
                
            exit;
            
        }
        exit;
        
    }
    
    
    
    // public media page
    function index($date=null){
        
        
        
        // call handler
        $this->__media(array(
            'date'=>$date,
            'recursive'=>true,
            'id'=>false,
        ));
        
        
        $this->set(compact('date'));
    
    }
    
    // media details page
    function gallery($id){
        
        // call handler
        $this->__media(array(
            'date'=>null,
            'id'=>$id,
        ));
        
    }
     
    
}
?>